#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>

typedef struct sockaddr * (SA);

int show()
{
	printf("----------menu----------\n");
	printf("1.设置城市\n");
	printf("2.实时天气\n");
	printf("3.未来天气\n");
	printf("4.历史天气\n");
	printf("5.退出\n");
	int i = 0;
	printf("input:");
	scanf("%d",&i);
	return i;
}

void request_header_comm(int conn)
{
	int i = 0;
	char *cmd_buf[6];
	cmd_buf[0] = "Host: api.k780.com\r\n";
	cmd_buf[1] = "User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1\r\n";
	cmd_buf[2] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
	cmd_buf[3] = "Accept-Language: en-us,en;q=0.5\r\n";
	cmd_buf[4] = "Accept-Encoding: gzip, deflate\r\n";
	cmd_buf[5] = "Connection: keep-alive\r\n\r\n";
	for(i = 0;i < 6;++i)
	{
		send(conn,cmd_buf[i],strlen(cmd_buf[i]),0);
	}
}

void request_header_sp(int conn,char *city,char *type)
{ 
	char buf[128] = {0};
	if(strlen(city) != 0)
	{
		sprintf(buf,"GET /?app=weather.%s&cityNm=%s&appkey=60399&sign=f5af5ca884ddef9bdb8ba6ee21117c10&format=json HTTP/1.1\r\n",type,city);
	}
	else
	{
		sprintf(buf,"GET /?app=weather.%s&cityNm=西安&appkey=60399&sign=f5af5ca884ddef9bdb8ba6ee21117c10&format=json HTTP/1.1\r\n",type);
	}
	send(conn,buf,strlen(buf),0);
}

char *deal(char*buf,int type)
{
	int i = 0;
	char* info[5];
	char use[5][15] = {"days","week","citynm","temperature","weather"};
	char use1[5][15] = {"week","citynm","uptime","temperature","weather"};
	for(i = 0;i < 5;++i)
	{
		if(i == 0)
		{
			if(type == 0)
			{
				info[i] = strstr(buf,use[i]);
			}
			else
			{
				info[i] = strstr(buf,use1[i]);
			}
		}
		else
		{
			if(type == 0)
			{
				info[i] = strstr(info[i-1],use[i]);
			}
			else
			{
				info[i] = strstr(info[i-1],use1[i]);
			}
		}
	}
	for(i = 0;i < 5;++i)
	{
		if(type == 0)
		{
			info[i] += (strlen(use[i]) + 3);
		}
		else
		{
			info[i] += (strlen(use1[i]) + 3);
		}
		char *p = info[i];
		p = index(p,'"');
		*p = '\0';
	}
	sprintf(buf,"%s %s %s %s %s",info[0],info[1],info[2],info[3],info[4]);
	return buf;
}

char deal_first(char *buf,int flag)
{
	char *p[30];
	int i = 0,j = 0;
	for(i = 0;i < 30;++i)
	{
		if(i == 0)
		{
			p[0] = strtok(buf,"{");
			if(p[0] ==NULL)
			{
				break;
			}
		}
		else
		{
			p[i] = strtok(NULL,"{");
			if(p[i] == NULL)
			{
				break;
			}
		}
	}
	if(flag == 0)
	{
		for(j = 2;j < i;++j)
		{
			printf("%s\n",deal(p[j],flag));
		}
	}
	else
	{
		for(j = 2;j < i;++j)
		{
			printf("%s\n",deal(p[j],flag));
		}
	}
}

char * recv_from(char *buf,char *buf1,int conn)
{
	while(1)
	{
		recv(conn,buf1,1024*10,0);
		strcat(buf,buf1);
		if(strstr(buf1,"\r\n0\r\n"))
		{
			break;
		}
	}
	return buf;
}

int main(int argc, const char *argv[])
{
	int conn = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == conn)
	{
		perror("socket");
		exit(1);
	}
	struct sockaddr_in ser;
	bzero(&ser,sizeof(ser));
	ser.sin_family = AF_INET;
	ser.sin_port = htons(80);
	ser.sin_addr.s_addr = inet_addr("103.205.5.249");
	int ret = connect(conn,(SA)&ser,sizeof(ser));
	if(-1 == ret)
	{
		perror("connect");
		exit(1);
	}
	
	char *buf = malloc(1024*10);
	char *buf1 = malloc(1024*10);
	char city[128] = {0};
	while(1)
	{
		int ret = show();
		if(ret == 1)
		{
			bzero(city,sizeof(city));
			printf("input city:");
			scanf("%s",city);
		}
		else if(ret == 2)
		{
			request_header_sp(conn,city,"today");
			request_header_comm(conn);
			bzero(buf,1024*10);
			bzero(buf1,1024*10);
			printf("%s\n",deal(recv_from(buf,buf1,conn),0));
		}
		else if(ret == 3)
		{
			request_header_sp(conn,city,"future");
			request_header_comm(conn);
			bzero(buf,1024*10);
			bzero(buf1,1024*10);
			deal_first(recv_from(buf,buf1,conn),0);
		}
		else if(ret == 4)
		{
			char date[10] = {0};
			printf("input date:");
			scanf("%s",date);
			bzero(buf,1024*10);
			if(strlen(city) != 0)
			{
				sprintf(buf,"GET /?app=weather.history&cityNm=%s&date=%s&appkey=60399&sign=f5af5ca884ddef9bdb8ba6ee21117c10&format=json HTTP/1.1\r\n",city,date);
			}
			else
			{
				sprintf(buf,"GET /?app=weather.history&cityNm=西安&date=%s&appkey=60399&sign=f5af5ca884ddef9bdb8ba6ee21117c10&format=json HTTP/1.1\r\n",date);
			}
			send(conn,buf,strlen(buf),0);
			request_header_comm(conn);
			bzero(buf,1024*10);
			bzero(buf1,1024*10);
			deal_first(recv_from(buf,buf1,conn),1);
		}
		else if(ret == 5)
		{
			printf("----------BYE----------\n");
			break;
		}
		else
		{
			printf("!!!NOT FOUND!!!\n");
		}
	}
	free(buf);
	free(buf1);
	close(conn);
	return 0;
}




